package com.ftg.learn.exam.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

import javax.servlet.http.HttpServletRequest;

/**
 * @author KangJx
 * @date 2021/4/2
 */
@Slf4j
@RestControllerAdvice
public class KjxRestExceptionGobal extends ResponseEntityExceptionHandler {

    /**
     * 处理自定义的业务异常
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value = KjxException.class)
    public KjxResponseEntity bizExceptionHandler(HttpServletRequest req, KjxException e){
        log.error("发生业务异常！原因是：{}",e.getMsg());
        return new KjxResponseEntity(e.getCode(),e.getMsg());
    }

    /**
     * 空指针异常
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value =NullPointerException.class)
    public KjxResponseEntity exceptionHandler(HttpServletRequest req, NullPointerException e){
        log.error("发生空指针异常！原因是:",e);
        return new KjxResponseEntity(ErrorCode.NULLPOINTER);
    }


    /**
     * 运行时异常
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value =RuntimeException.class)
    public KjxResponseEntity exceptionHandler(HttpServletRequest req, RuntimeException e){
        log.error("未知异常！原因是:",e);
        return new KjxResponseEntity(ErrorCode.RuntimeException);
    }
}
